feat(firestore): literals pipeline stage#16028
feat(firestore): literals pipeline stage#16028Linchin wants to merge 7 commits intogoogleapis:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the Firestore client library by introducing a new Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new literals pipeline stage, which allows specifying a fixed set of documents as the starting point of a pipeline. The implementation includes the literals method on the pipeline builder, the Literals stage class, and corresponding unit and end-to-end tests. My review focuses on improving the clarity and correctness of the type hints and docstrings for the new functionality. I've suggested changes to make the API easier to understand and use correctly.
| stages.FindNearest(field, vector, distance_measure, options) | ||
| ) | ||
|
|
||
| def literals(self, *documents: str | Selectable) -> "_BasePipeline": |
There was a problem hiding this comment.
The type hint for *documents is incomplete. It should include dict as documents are often passed as dictionaries, which is not covered by str | Selectable.
| def literals(self, *documents: str | Selectable) -> "_BasePipeline": | |
| def literals(self, *documents: dict | str | Selectable) -> "_BasePipeline": |
There was a problem hiding this comment.
You can probably ignore this, unless that's how other languages handle it
There was a problem hiding this comment.
Actually, looking at go, it seems like it accepts dicts, but not strings?
I don't know much about this stage, but from what I've seen, it's supposed to deal with maps. So maybe this should be def literals(self, *documents: Map | dict[str, CONSTANT_TYPE] | Selectable)?
There was a problem hiding this comment.
Upon further thoughts, I think it should be def literals(self, *documents: dict | Expression):. In this case both Constant and Map are child classes of Expression.
| documents: A `str` or `Selectable` expression. If a `str`, it's | ||
| treated as a field path to an array of documents. | ||
| If a `Selectable`, it's usually a `Constant` | ||
| containing an array of documents (as dictionaries). |
There was a problem hiding this comment.
The Args section of the docstring is misleading. It describes a single argument, but the method accepts *documents. It also incorrectly states that a str is treated as a field path; the implementation treats it as a string literal. To use a field path, field() should be used explicitly.
| documents: A `str` or `Selectable` expression. If a `str`, it's | |
| treated as a field path to an array of documents. | |
| If a `Selectable`, it's usually a `Constant` | |
| containing an array of documents (as dictionaries). | |
| documents: One or more documents to be returned by this stage. | |
| Each can be a `dict`, a `str` literal, or a | |
| `Selectable` expression (e.g. `field("path")` or | |
| `Constant.of(...)`). |
There was a problem hiding this comment.
+1. Usually I'd put the * in here too: *documents:
There was a problem hiding this comment.
Updated the docstring to be more accurate.
packages/google-cloud-firestore/google/cloud/firestore_v1/pipeline_stages.py
Outdated
Show resolved
Hide resolved
| documents: A `str` or `Selectable` expression. If a `str`, it's | ||
| treated as a field path to an array of documents. | ||
| If a `Selectable`, it's usually a `Constant` | ||
| containing an array of documents (as dictionaries). |
There was a problem hiding this comment.
+1. Usually I'd put the * in here too: *documents:
| stages.FindNearest(field, vector, distance_measure, options) | ||
| ) | ||
|
|
||
| def literals(self, *documents: str | Selectable) -> "_BasePipeline": |
There was a problem hiding this comment.
You can probably ignore this, unless that's how other languages handle it
| pipeline: | ||
| - Literals: | ||
| - title: "The Hitchhiker's Guide to the Galaxy" | ||
| author: "Douglas Adams" |
There was a problem hiding this comment.
Maybe it's been a while since I looked at this, but this doesn't seem like the right syntax to me. Isn't this essentially sending Literals({"title": ""The Hitchhiker's Guide to the Galaxy", "author", "Douglas Adams"})? That doesn't seem to match the str | Selectable
Does the test pass?
There was a problem hiding this comment.
It does pass. I think Literals should be able to accept dicts, as well as other Expression types. I have updated the system test to include dict and Constant (a child class of Expression).
| ... {"name": "alice", "age": 40} | ||
| ... ] | ||
| >>> pipeline = client.pipeline() | ||
| ... .literals(Constant.of(documents)) |
There was a problem hiding this comment.
Looking at the code, it seems like:
- Constant isn't a Selectable
- Constant doesn't seem like it supports dict types. (We do have a Map, which serves that purpose, but it doesn't seem Selectable either)
There was a problem hiding this comment.
Thanks for catching this! I spent some time to check the internal docs, and I think Expression class and dict should be supported per the following language:
While literal values are the most common, it is also possible to pass in
expressions, which will be evaluated and returned, making it possible to test
out different query / expression behavior without first needing to create some
test data.
| stages.FindNearest(field, vector, distance_measure, options) | ||
| ) | ||
|
|
||
| def literals(self, *documents: str | Selectable) -> "_BasePipeline": |
There was a problem hiding this comment.
Actually, looking at go, it seems like it accepts dicts, but not strings?
I don't know much about this stage, but from what I've seen, it's supposed to deal with maps. So maybe this should be def literals(self, *documents: Map | dict[str, CONSTANT_TYPE] | Selectable)?
| val2 = Constant.of({"b": 2}) | ||
| instance = self._make_one(val1, val2) | ||
| assert instance.documents == (val1, val2) | ||
| assert instance.name == "literals" |
There was a problem hiding this comment.
We should have tests that cover all supported input types. I don't see anything using str (and looking at go, I'm not sure if we should be supporting str?)
There was a problem hiding this comment.
Indeed, I don't think we should support str. Here the test includes dict and Constant, which serves as an example of Expression's child class. It seems too cumbersome to test for each and every child class of Expression? What do you think.
| stringValue: "Douglas Adams" | ||
| title: | ||
| stringValue: "The Hitchhiker's Guide to the Galaxy" | ||
| name: literals No newline at end of file |
There was a problem hiding this comment.
We should also have tests here that cover the different input types we support
There was a problem hiding this comment.
Good catch! I added additional type to test.
Succeeding googleapis/python-firestore#1170 for the monorepo migration.